﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using Microsoft.SharePoint.Administration;
using Trentacular.SharePoint.StsAdmin.Util;

namespace Trentacular.SharePoint.StsAdmin
{
    public class DeploymentManager : BaseConsoleHelper
    {
        private JobDefinitionExecuter _jobDefinitionExecuter;
        public JobDefinitionExecuter JobDefinitionExecuter
        {
            get
            {
                if (_jobDefinitionExecuter == null)
                    _jobDefinitionExecuter = new JobDefinitionExecuter();
                return _jobDefinitionExecuter;
            }
            set { _jobDefinitionExecuter = value; }
        }

        public void RetractAllSolutionDeployments(SPSolution solution)
        {
            if (!solution.ContainsWebApplicationResource)
            {
                DoConsoleAction(string.Format("Retracting the {0} solution globally", solution.Name), delegate()
                {
                    solution.Retract(DateTime.Now);
                });

                JobDefinitionExecuter.ExecuteJobDefinitions();
            }
            else
            {
                foreach (var webApp in solution.DeployedWebApplications)
                {
                    DoConsoleAction(string.Format("Retracting the {0} solution from {1}", solution.Name, webApp.GetUrl()), delegate()
                    {
                        var selectedWebApps = new Collection<SPWebApplication>();
                        selectedWebApps.Add(webApp);
                        solution.Retract(DateTime.Now, selectedWebApps);
                    });

                    JobDefinitionExecuter.ExecuteJobDefinitions();
                }
            }
        }

        public void DeleteSolution(SPSolution solution)
        {
            DoConsoleAction(string.Format("Deleting the {0} solution", solution.Name), delegate()
            {
                SPFarm.Local.Solutions.Remove(solution.Id);
            });
        }

        public SPSolution AddSolution(string solutionPath)
        {
            var solutionFilename = Path.GetFileName(solutionPath);
            SPSolution solution = null;

            DoConsoleAction(string.Format("Adding the {0} solution", solutionFilename), delegate()
            {
                solution = SPFarm.Local.Solutions.Add(solutionPath);
            });

            return solution;
        }

        public void DeploySolution(SPSolution solution, params Guid[] webApplicationIds)
        {
            if (!solution.ContainsWebApplicationResource)
            {
                DoConsoleAction(string.Format("Deploying the {0} solution globally", solution.Name), delegate()
                {
                    solution.Deploy(DateTime.Now, true, true);
                });

                JobDefinitionExecuter.ExecuteJobDefinitions();
            }
            else
            {
                foreach (var webApplicationId in webApplicationIds)
                {
                    var webApplication = SPUtil.GetWebApplication(webApplicationId);
                    DoConsoleAction(string.Format("Deploying the {0} solution to {1}", solution.Name, webApplication.GetUrl()), delegate()
                    {
                        var selectedWebApps = new Collection<SPWebApplication>();
                        selectedWebApps.Add(webApplication);
                        solution.Deploy(DateTime.Now, true, selectedWebApps, true);
                    });

                    JobDefinitionExecuter.ExecuteJobDefinitions();
                }
            }
        }
    }
}
